package cfdt.ldl.signatrue.service;

import cfdt.common.enums.CommonCodeEnum;
import cfdt.common.utils.BeanUtils;
import cfdt.common.vo.ObjectBaseResponse;
import cfdt.ldl.signatrue.service.bo.BaseSignature;
import cfdt.ldl.signatrue.service.bo.BaseVerifier;
import cfdt.ldl.signature.api.request.SignatrueRequest;
import cfdt.ldl.signature.api.response.SignatureResponse;
import cfdt.tools.util.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;


@Service
public class SignatureService{

    private final Logger logger = LoggerFactory.getLogger(getClass().getName());

    // 签名
    public ObjectBaseResponse<SignatureResponse> sign(SignatrueRequest request) {
        // 交易中心机构
        String tradeCenterInstitutionID = request.getTradeCenterInstitutionId();
        // 获取实现类
        String beanName = "signatrue" + StringUtils.trim(tradeCenterInstitutionID);
        BaseSignature baseSignature = null;
        try {
            baseSignature = BeanUtils.getBean(beanName, BaseSignature.class);
        } catch (Exception e) {
            logger.error("获取" + beanName + "实例异常", e);
            return ObjectBaseResponse.error(CommonCodeEnum.CODE_1003);
        }

        return baseSignature.sign(request);
    }

    // 验签
    public ObjectBaseResponse<SignatureResponse> verify(SignatrueRequest request) {
        // 交易中心机构
        String tradeCenterInstitutionID = request.getTradeCenterInstitutionId();
        // 获取实体类
        String beanName = "verify" + StringUtils.trim(tradeCenterInstitutionID);
        BaseVerifier baseSignature = null;
        try {
            baseSignature = BeanUtils.getBean(beanName, BaseVerifier.class);
        } catch (Exception e) {
            logger.error("获取" + beanName + "实例异常", e);
            return ObjectBaseResponse.error(CommonCodeEnum.CODE_1003);
        }

        return baseSignature.verify(request);
    }
}
